﻿using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

namespace WebApiSecurity.Tests
{
    [TestFixture]
    public class Step02
    {
        [Test]
        public async Task Can_call_the_api()
        {
            var client = new HttpClient(new AuthenticationHandler()) {BaseAddress = new Uri("http://localhost.fiddler:49623/")};
            var response = await client.GetAsync("api/values");
            var result = await response.Content.ReadAsAsync<string[]>();

            foreach (var value in result) Console.WriteLine(value);
        }

        public class AuthenticationHandler : DelegatingHandler
        {
            public AuthenticationHandler() : base(new HttpClientHandler())
            {
            }

            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                request.Headers.Add("Demo-Key", "Woot!  I'm in");
                return base.SendAsync(request, cancellationToken);
            }
        }
    }
}
